/*
 *                  BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 * 
 * Created on Oct 11, 2007
 * 
 */

package org.biojava.services.das.registry;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import javax.xml.namespace.QName;

import org.apache.axis.Constants;
import org.apache.axis.encoding.DeserializationContext;
import org.apache.axis.encoding.Deserializer;
import org.apache.axis.encoding.DeserializerFactory;
import org.apache.axis.encoding.DeserializerImpl;
import org.apache.axis.encoding.FieldTarget;
import org.apache.axis.encoding.Target;
import org.apache.axis.message.SOAPHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public class PropertiesDeserializerFactory implements DeserializerFactory {

	private  Vector mechanisms;

	 public PropertiesDeserializerFactory() {
	
	}
	public  javax.xml.rpc.encoding.Deserializer getDeserializerAs(String mechanismType) {
		return  new PropertiesDeserializer();
	}
	public  Iterator getSupportedMechanismTypes() {
		if  (mechanisms == null) {
			mechanisms = new  Vector();
			mechanisms.add(Constants.AXIS_SAX);
		}
		return  mechanisms.iterator();
	}



}


class PropertiesDeserializer extends DeserializerImpl{

	private  Hashtable typesByMemberName = new Hashtable();  
	public  static final QName myTypeQName = new QName("typeNS", "Properties");
	
	public PropertiesDeserializer(){
		typesByMemberName.put("key",  Constants.XSD_STRING);
		typesByMemberName.put("value",  Constants.XSD_STRING);
	}

	/**
	 * This  method is invoked when an element start tag is encountered.
	 * @param  namespace is the namespace of the element
	 * @param  localName is the name of the element
	 * @param  prefix is the element's prefix
	 * @param  attributes on the element...used to get the type
	 * @param  context is the DeserializationContext
	 */
	public  SOAPHandler onStartChild(String namespace,
			String  localName,
			String  prefix,
			Attributes  attributes,
			DeserializationContext  context)
	throws  SAXException
	{
		
		System.out.println("serializing " + namespace + "  " + localName );
		QName  typeQName = (QName)typesByMemberName.get(localName);
		if  (typeQName == null)
			throw  new SAXException("Invalid element in Properties struct - " + localName);

		// These  can come in either order.
		Deserializer  dSer = context.getDeserializerForType(typeQName);
		try {
			dSer.registerValueTarget(new  FieldTarget(value, localName));
		} catch  (NoSuchFieldException e) {
			throw  new SAXException(e);
		}

		if (dSer == null)
			throw  new SAXException("No deserializer for a " + typeQName + "???");

		return  (SOAPHandler)dSer;
	}


}